package org.msb.accentis.data.pojo;

import java.math.BigDecimal;

import javax.persistence.Basic;
import javax.persistence.Entity;
import javax.persistence.Lob;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;

@Entity
@NamedQueries({
		@NamedQuery(name = "TransactionDetail.findTransactionDetailsForAccount", query = "SELECT td FROM TransactionDetail td WHERE td.account = :account"),
		@NamedQuery(name = "TransactionDetail.getAccountBalance", query = "SELECT SUM(td.amount) FROM Transaction t, IN(t.details) td WHERE td.account = :account") })
public class TransactionDetail extends BaseObject implements Cloneable {

	@ManyToOne
	private Individual individual;

	@Basic
	@Lob
	private String description;

	@ManyToOne
	private Account account;

	@Basic
	private BigDecimal amount = BigDecimal.ZERO;

	@ManyToOne
	private Payee payee;

	@ManyToOne
	private Category category;

	public Individual getIndividual() {
		return individual;
	}

	public void setIndividual(Individual value) {
		individual = value;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String value) {
		description = value;
	}

	public Account getAccount() {
		return account;
	}

	public void setAccount(Account value) {
		account = value;
	}

	public BigDecimal getAmount() {
		return amount;
	}

	public void setAmount(BigDecimal value) {
		amount = value;
	}

	public Payee getPayee() {
		return payee;
	}

	public void setPayee(Payee value) {
		payee = value;
	}

	public Category getCategory() {
		return category;
	}

	public void setCategory(Category value) {
		category = value;
	}
	
	public BigDecimal getValue() {
		BigDecimal amount = getAmount();
		Category category = getCategory();
		
		if (null != category && category.isExpense())
			amount = amount.negate();
		
		return amount;
	}
	
	public TransactionDetail clone() {
		try {
			return (TransactionDetail) super.clone();
		} catch (CloneNotSupportedException ex) {
			return null;
		}
	}
}
